package com.china08.yxyapi.api.manage.stu.mystudy.huixue.studyrecord;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.huixue.YxyHuixuePlan;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanRepository;

/**
 * 学习记录
 * 
 */
@CrossOrigin
@RestController
public class HuiXueStudyRecoredController extends YxyApiBaseController {

    @Autowired
    private YxyHuixuePlanRepository yxyHuixuePlanRepository;

    @RequestMapping(value = "/api_yw/manage/stu/mystudy/huixue/studyrecored/recored/list", method = RequestMethod.POST)
    public Page<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) throws ParseException {
         Pageable pageable = new PageRequest(reqModel.page, reqModel.size);
        Page<YxyHuixuePlan> yxyHuixuePlanLst = yxyHuixuePlanRepository
                .findByStudentIdAndStatusAndCurrentScoreGreaterThanOrderByLastModifiedDateDesc(reqModel.studentId, 0, -1,pageable);
        List<RespModel> models = new ArrayList<RespModel>();
        RespModel model = null;
        for (YxyHuixuePlan yxyHuixuePlan : yxyHuixuePlanLst) {
            model = new RespModel();
            model.planId = yxyHuixuePlan.getId();
            model.subjectName = yxyHuixuePlan.getSubjectName();
            model.bookContentName = yxyHuixuePlan.getBookContentName();
            // 开始时间
            Integer beginDate = yxyHuixuePlan.getBeginDate();
            String beginDateString = null;
            if (beginDate != null && beginDate != 0) {
                beginDateString = String.valueOf(beginDate);
                beginDateString = beginDateString.substring(0, 4) + "-" + beginDateString.substring(4, 6) + "-"
                        + beginDateString.substring(6);
            }
            // 结束时间
            Integer endDate = yxyHuixuePlan.getEndDate();
            String endDateString = null;
            if (endDate != null && endDate != 0) {
                endDateString = String.valueOf(endDate);
                endDateString = endDateString.substring(0, 4) + "-" + endDateString.substring(4, 6) + "-"
                        + endDateString.substring(6);
            }
            model.beginDate = beginDateString;
            model.endDate = endDateString;
            model.status = yxyHuixuePlan.getStatus();
            model.progress = yxyHuixuePlan.getProgress();
            if (model.progress == 100) {
                model.comment = "恭喜您，完成了本次学习计划！";
            } else if (model.progress < 100) {
                model.comment = "很遗憾，你没有完成既定的学习计划。不要气馁，继续努力！";
            }
            model.studentId = reqModel.studentId;
            models.add(model);
        }
         PageImpl<RespModel> pageImpl=new
         PageImpl<RespModel>(models,pageable,yxyHuixuePlanLst.getTotalElements());
         Page<RespModel> pageLst = pageImpl;
        return pageLst;

    }

    protected static class RespModel extends YxyApiRespModel {
        // 学习计划id
        public String planId;
        // 科目名称
        public String subjectName;
        // 章节名称
        public String bookContentName;
        // 开始时间
        public String beginDate;
        // 结束时间
        public String endDate;
        // 完成度
        public Double progress;
        // 学习计划的状态
        public Integer status;
        // 当前计划点评
        public String comment;
        // 学生id
        public String studentId;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        // 学生id
        public String studentId;
    }
}
